﻿#include "tools.h"
#include "md5.h"
#include "sha1.h"
#include <stdio.h>

TOOL_API int STDCALL Md5Computer( const uint8_t *input, int32_t ilen, char output[33] )
{
    uint8_t md5sum[16] = {0};

    int nRet = ex_md5_ret(input, ilen, md5sum);
    if (TOOLS_OK == nRet)
    {
        for (int i = 0; i < sizeof(md5sum); ++i)
        {
            sprintf(&(output[2*i]),"%02X",(unsigned char)md5sum[i]);
            sprintf(&(output[2*i+1]),"%02X",(unsigned char)(md5sum[i]<<4));
        }
        output[32]='\0';
    }

    return nRet;
}

TOOL_API int STDCALL Md5FileComputer( TFileParam &tMd5FileParam )
{
    uint8_t md5sum[16] = {0};

    int nRet = ex_md5_file_ret(tMd5FileParam, md5sum);
    if (TOOLS_OK == nRet)
    {
        for (int i = 0; i < sizeof(md5sum); ++i)
        {
            sprintf(&(tMd5FileParam.output[2*i]),"%02X",(unsigned char)md5sum[i]);
            sprintf(&(tMd5FileParam.output[2*i+1]),"%02X",(unsigned char)(md5sum[i]<<4));
        }
        tMd5FileParam.output[32]='\0';
    }

    return nRet;
}

TOOL_API int STDCALL Sha1Computer( const uint8_t *input, int32_t ilen, char output[41] )
{
    uint8_t sha1sum[20] = {0};

    int nRet = ex_sha1_ret(input, ilen, sha1sum);
    if (TOOLS_OK == nRet)
    {
        for(int i = 0; i < sizeof(sha1sum); ++i)
        {
            sprintf(&(output[2*i]),"%02X",(unsigned char)sha1sum[i]);
        }
        output[40]='\0';
    }

    return nRet;
}

TOOL_API int STDCALL Sha1FileComputer( TFileParam &tMd5FileParam )
{
    uint8_t sha1sum[20] = {0};

    int nRet = ex_sha1_file_ret(tMd5FileParam, sha1sum);
    if (TOOLS_OK == nRet)
    {
        for(int i = 0; i < sizeof(sha1sum); ++i)
        {
            sprintf(&(tMd5FileParam.output[2*i]), "%02X", (unsigned char)sha1sum[i]);
        }
        tMd5FileParam.output[40]='\0';
    }

    return nRet;
}
